﻿using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PluginInstaller
{
    public partial class InfoDialog : Form
    {
        public void ShowMessage(string text)
        {
            text = text + Environment.NewLine;
            if (InvokeRequired) {
                Invoke(new Action(() => txtMsg.AppendText(text)));
            }
            else {
                txtMsg.AppendText(text);
            }
        }

        public InfoDialog()
        {
            InitializeComponent();
        }

        internal Installer Installer { get; set; }

        private void InfoDialog_Load(object sender, EventArgs e)
        {
            btnClose.Enabled = false;
            Installer.ShowMessage = ShowMessage;
            Installer.Done = result => {
                if (result) {
                    if (InvokeRequired) {
                        Invoke(new Action(Close));
                    }
                    else {
                        Close();
                    }
                }
                else {
                    if (InvokeRequired) {
                        Invoke(new Action(() => {
                            btnClose.Enabled = true;
                        }));
                    }
                    else {
                        btnClose.Enabled = true;
                    }
                }
            };
            Task.Run(() => Installer.Install());
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
